<?php
namespace app\common\service\admin;

use app\common\dao\AdminDao;
use app\common\dao\LogDao;
use app\common\dao\AdminAuthorizeDao;
use app\common\service\BaseService;
use app\common\utils\Unique;
use think\facade\Session;

/**
 * 后台管理员
 */
class AdminService extends BaseService
{
    /**
     * 管理员登录
     * 
     * @param array $param 表单内容
     * @return array
     */
    public function login($param)
    {
        if (empty($param['username'])) {
            throw new \think\Exception('请输入用户名');
        }
        if (empty($param['password'])) {
            throw new \think\Exception('请输入密码');
        }

        $adminDao = new AdminDao();
        $admin    = $adminDao->getByUsername($param["username"]);
        if (empty($admin)) {
            throw new \think\Exception('用户名错误');
        }
        if ($admin["password"] !== sha1($param["password"])) {
            throw new \think\Exception('密码错误');
        }
        $admin = $admin->toArray();

        // 获取管理员auth
        $auth = $adminDao->getAdminAuth($admin['id']);
        if (empty($auth)) {
            throw new \think\Exception('生成Token出错');
        }

        // 登录成功
        Session::set("admin_auth_id", $admin['id']);

        // 日志
        // LogDao::add("管理员登录", 1, "登录成功", json_encode($param, JSON_UNESCAPED_UNICODE));

        return [
            "id"       => $admin['id'],
            "username" => $admin['username'],
            "auth"     => $auth,
        ];
    }

    /**
     * 获取管理员信息
     * 
     * @param  int $id
     * @return array
     */
    public function getAdminInfo($id)
    {
        $admin = (new AdminDao())->trailGetById($id);
        if (empty($admin)) {
            throw new \think\Exception('管理员不存在');
        }
        $admin = $admin->toArray();

        // 脱敏
        unset($admin['password']);

        return $admin;
    }

    /**
     * 添加管理员
     *
     * @param array $param 表单内容
     * @return array
     */
    public function register($param)
    {
        if (empty($param['username'])) {
            throw new \think\Exception('帐号不能为空');
        }

        // 密码与确认密码是否一致
        if (sha1($param['password']) !== sha1($param['password2'])) {
            throw new \think\Exception('请确认密码一致');
        }

        // 是否已存在相同帐号
        $has = (new AdminDao())->where([
            ["username", "=", $param['username']],
        ])->find();
        if (!empty($has)) {
            throw new \think\Exception('帐号已存在，请重新填写新帐号');
        }

        // 注册
        $result = (new AdminDao())->create([
            "username" => $param['username'],
            "password" => sha1($param['password']),
            "nickname" => "管理" . substr(md5($param['username']), 0, 5),
            "remark"   => $param['remark'] ?? "",
        ]);
        if (!$result) {
            throw new \think\Exception('注册出现错误，请重新提交');
        }

        // 获取管理员auth
        $auth = (new AdminDao())->getAdminAuth($result->id);
        if (empty($auth)) {
            throw new \think\Exception('生成Token出错');
        }

        // 日志
        // LogDao::add("管理员注册", 1, "注册成功", json_encode($param, JSON_UNESCAPED_UNICODE));

        return [
            "id"    => $result->id,
            "auth"  => $auth,
        ];
    }

    /**
     * 返回所有管理员列表
     * 
     * @return array
     */
    public function getAdminAll()
    {
        $list = (new AdminDao())->order("id DESC")->select()->toArray();

        // 过滤
        $array = [];
        foreach ($list as $key => $value) {
            unset($value['password']);

            $array[$key] = $value;
        }

        return $array;
    }
}
